package com.estar.integaration.Reader.Listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.estar.integaration.Util.Util;
import com.estar.integaration.entity.paymentPlanEntity.PurchaseOrderDetail;
import lombok.AllArgsConstructor;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: YJL
 * Date: 2022/2/17 16:44
 **/

public class PurchaseOrderDetailListener extends AnalysisEventListener<PurchaseOrderDetail> {

  List<PurchaseOrderDetail> purchaseOrderDetailList = new ArrayList<>();

  String thisOrderNumber = "";

  String thisPurchaseGroup = "";

  PurchaseOrderDetail thisPod = null;

  public PurchaseOrderDetailListener(List<PurchaseOrderDetail> purchaseOrderDetailList) {
    this.purchaseOrderDetailList = purchaseOrderDetailList;
  }

  @Override
  public void invoke(PurchaseOrderDetail purchaseOrderDetail, AnalysisContext analysisContext) {
    if(purchaseOrderDetail == null)
      return;
    if(purchaseOrderDetail.getSupplierName() == null || purchaseOrderDetail.getSupplierName().equals(" ")
        || purchaseOrderDetail.getSupplierName().equals("")
    ){
      System.out.println("供应商名称为空，为其他物料，不解析");
      return;
    }
    if(purchaseOrderDetail.getMaterialNumber().startsWith("G")){
      return;//为G的不解析
    }
//    if(purchaseOrderDetail.getOrderNumber().equals("SIT-P20210511-03"))
//      System.out.println("读取Excel Debug");
    setNullValue(purchaseOrderDetail);

    /**
    //测试同物料合并
    if(purchaseOrderDetail.getMaterialNumber().equals("B04002010207") && purchaseOrderDetail.getSupplierName().equals("菱创智能科技（常熟）有限公司") && purchaseOrderDetail.getInventoryVoucherNumber().equals("CGRK210927022")){
      System.out.println("test");
    }
    //第二个
    if(purchaseOrderDetail.getMaterialNumber().equals("B04002010207") && purchaseOrderDetail.getSupplierName().equals("菱创智能科技（常熟）有限公司") && purchaseOrderDetail.getInventoryVoucherNumber().equals("CGRK210918002")){
      System.out.println("test");
    }**/

    /**
     *  价税合计为空的找到上一个价税合计，
     *  并且把 入库数量,入库金额，应付数量，应付金额，付款金额相加
     *  优先级高于空orderNumber，因为是同一个物料号，搞完直接解析回去
     */
    if(purchaseOrderDetail.getPriceTaxGather() == null){

      //非正常情况，价税合计不空,进行合并
      if(purchaseOrderDetail.getDueNumber() != null && purchaseOrderDetail.getDueNumber().equals(thisPod.getDueNumber())){//dueNumber不为空,且和thisPod相等，则合并
        thisPod = fixSamePOD(thisPod,purchaseOrderDetail);
        return;//这个已经在list里面了，直接解析下一个
      }else if(purchaseOrderDetail.getDueNumber() != null && !purchaseOrderDetail.getDueNumber().equals(thisPod.getDueNumber())){//应付单号不相等
        //不相等 -> 两个值都存在 不等  原存在新不存在不等(其他分支) 原存在新存在不等(接着向下走) 原不存在新不存在(这边新处理)
        if(thisPod.getDueNumber() == null){//原来的是null,又不等，说明purchaseOrderDetail有值
          thisPod = fixSamePOD(thisPod,purchaseOrderDetail);
        }else {//确实不同
          purchaseOrderDetail.setPriceTaxGather(0d);//不一样说明时间相差久，同一个物料分成两个应付单
        }
      }else if(purchaseOrderDetail.getDueNumber() == null  //同物料,同供应商,才合并
          && purchaseOrderDetail.getSupplierName().equals(thisPod.getSupplierName())
          && purchaseOrderDetail.getMaterialNumber().equals(thisPod.getMaterialNumber())
      ){//我是空，并且我和你是同一个供应商
        thisPod = fixSamePOD(thisPod,purchaseOrderDetail);
        return;//这个已经在list里面了，直接解析下一个
      }
    }else {//正常情况，如果价税合计不空就原流程继续走
      thisPod = purchaseOrderDetail;
    }

    if(purchaseOrderDetail.getOrderNumber() == null){
      if(!thisOrderNumber.equals("")){
        purchaseOrderDetail.setOrderNumber(thisOrderNumber);
      }
    }else if(purchaseOrderDetail.getOrderNumber().equals("")
        || purchaseOrderDetail.getOrderNumber().equals("null")
        || purchaseOrderDetail.getOrderNumber().equals(" ")
    ){
      if(!thisOrderNumber.equals("")){
        purchaseOrderDetail.setOrderNumber(thisOrderNumber);
      }
    }else {
      thisOrderNumber = purchaseOrderDetail.getOrderNumber();
    }
    //设置采购组
    setpurchaseGroup(purchaseOrderDetail,thisPurchaseGroup);
    purchaseOrderDetail.setId(Util.getUUID());
    purchaseOrderDetail.setExpectPayDate("");
    if(purchaseOrderDetail.getPayAmount() == null)
      purchaseOrderDetail.setPayAmount(0.0);

    if(purchaseOrderDetail.getOrderNumber().equals("SIT-P20220121-09")){
      System.out.println("t");
    }

    purchaseOrderDetailList.add(purchaseOrderDetail);



  }


  @Override
  public void doAfterAllAnalysed(AnalysisContext analysisContext) {

  }

  /**
   * 合并相同POD
   * 价税合计是空的，同一个物料进行合并
   * @param
   * @return
   */
  private PurchaseOrderDetail fixSamePOD(PurchaseOrderDetail thisPod,PurchaseOrderDetail newPOD){
    //合并入库金额
    if(newPOD.getInventoryMoney() != null){
      thisPod.setInventoryMoney(thisPod.getInventoryMoney() + newPOD.getInventoryMoney());
    }
    //合并应付金额
    if(newPOD.getAmountDue() != null){
      thisPod.setAmountDue(thisPod.getAmountDue() + newPOD.getAmountDue());
    }
    //合并付款金额
    if(newPOD.getPayAmount() != null){
      thisPod.setPayAmount(thisPod.getPayAmount() + newPOD.getPayAmount());
    }
    //TODO 合并其他数据
    //入库日期
    if(thisPod.getInventoryDate() == null && newPOD.getInventoryDate() != null)
      thisPod.setInventoryDate(newPOD.getInventoryDate());
    //入库单号
    if(thisPod.getInventoryVoucherNumber() == null && newPOD.getInventoryVoucherNumber() != null){
      thisPod.setInventoryVoucherNumber(newPOD.getInventoryVoucherNumber());
    }
    //入库金额
    if(thisPod.getInventoryMoney() == null && newPOD.getInventoryMoney() != null){
      thisPod.setInventoryMoney(newPOD.getInventoryMoney());
    }
    //应付单号
    if(thisPod.getDueNumber() == null && newPOD.getDueNumber() != null){
      thisPod.setDueNumber(newPOD.getDueNumber());
    }
    //应付金额
    if(thisPod.getAmountDue() < 0.00001 && newPOD.getAmountDue() > 0.00001 ){
      thisPod.setAmountDue(newPOD.getAmountDue());
    }
    //付款金额
    if(thisPod.getPayAmount() < 0.0001 && newPOD.getPayAmount() > 0.00001){
      thisPod.setPayAmount(newPOD.getPayAmount());
    }
    //图号
    if(thisPod.getDrawingNo() == null && newPOD.getDrawingNo() != null){
      thisPod.setDrawingNo(newPOD.getDrawingNo());
    }
    return thisPod;
  }

  private void setNullValue(PurchaseOrderDetail purchaseOrderDetail){
    if(purchaseOrderDetail.getPredeterminedNumber() == null){
      purchaseOrderDetail.setPredeterminedNumber(0.0);
    }
    if(purchaseOrderDetail.getPriceWithTax() == null){
      purchaseOrderDetail.setPriceWithTax(0.0);
    }

    //有应付单号,但是没有应付金额
    if(purchaseOrderDetail.getAmountDue() == null){
      if(purchaseOrderDetail.getDueNumber() == null
          || purchaseOrderDetail.getDueNumber().equals(" ")
          || purchaseOrderDetail.getDueNumber().equals("")
      ){//应付单号为空的时候
        purchaseOrderDetail.setAmountDue(0.0);
      }else {//应付单号不为空
        Double inventoryMoney = purchaseOrderDetail.getInventoryMoney();
        if(inventoryMoney != null){//有入库金额
          purchaseOrderDetail.setAmountDue(inventoryMoney);
        }else {
          purchaseOrderDetail.setAmountDue(0.0d); //TODO 应该有很多需要兜底的物料，晚上排查一下
        }
      }
    }

    if(purchaseOrderDetail.getInventoryMoney() == null){
      purchaseOrderDetail.setInventoryMoney(0.0);
    }

    if(purchaseOrderDetail.getPayAmount() == null){
      purchaseOrderDetail.setPayAmount(0.0);
    }
  }

  private void setpurchaseGroup(PurchaseOrderDetail purchaseOrderDetail,String thisPurchaseGroup){
    if(purchaseOrderDetail.getPurchaseGroup() == null){
      purchaseOrderDetail.setPurchaseGroup(thisPurchaseGroup);
    }else if(purchaseOrderDetail.getPurchaseGroup().equals("")
    || purchaseOrderDetail.getPurchaseGroup().equals(" ")
    || purchaseOrderDetail.getPurchaseGroup().equals("null")
    ){
      purchaseOrderDetail.setPurchaseGroup(thisPurchaseGroup);
    }else {
      this.thisPurchaseGroup = purchaseOrderDetail.getPurchaseGroup();
    }
  }

}
